package com.tuanzi.loan.business.repository.statistics.impl;

import com.tuanzi.loan.business.entity.collaborate.StatCollaborateCheckIn;
import com.tuanzi.loan.business.repository.statistics.custom.StatCheckInCustomRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.statistics.login.StatCheckInRequest;
import com.tuanzi.loan.vo.response.statistics.login.StatCheckInResponse;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

import static com.tuanzi.loan.generated.jooq.loan.Tables.COMM_PRODUCT_PRDID;
import static com.tuanzi.loan.generated.jooq.loan.tables.CommProductChannel.COMM_PRODUCT_CHANNEL;
import static com.tuanzi.loan.generated.jooq.stat.tables.StatCheckIn.STAT_CHECK_IN;

/**
 * @Author: chenweijun
 * @Date: 2019/3/13 17:55
 * @Description:
 */
@Repository
public class StatCheckInRepositoryImpl extends JOOQBaseRepository implements StatCheckInCustomRepository{

    @Override
    protected TableImpl getTable() {
        return STAT_CHECK_IN;
    }

    @Override
    public Page<StatCheckInResponse> list(StatCheckInRequest request, Pageable pageable) {

        fields(STAT_CHECK_IN.CREATE_DATE,STAT_CHECK_IN.PLATFORM,STAT_CHECK_IN.PRODUCT_ID,STAT_CHECK_IN.CHANNEL_CODE,
                STAT_CHECK_IN.ACTIVITY_ID,COMM_PRODUCT_CHANNEL.CHANNEL_GROUP,COMM_PRODUCT_CHANNEL.SECONDARY_CHANNEL_GROUP, STAT_CHECK_IN.H5_VIEW_PAGE_COUNT,
                STAT_CHECK_IN.H5_CHECK_IN_COUNT,STAT_CHECK_IN.H5_REGISTER_COUNT_A,STAT_CHECK_IN.H5_REGISTER_COUNT_B,
                STAT_CHECK_IN.LOGIN_COUNT,STAT_CHECK_IN.APP_REGISTER_COUNT_A,STAT_CHECK_IN.APP_REGISTER_COUNT_B,
                STAT_CHECK_IN.UV_PHONE,STAT_CHECK_IN.UV_API,STAT_CHECK_IN.API_APPLY_COUNT,STAT_CHECK_IN.API_LEND_COUNT,
                STAT_CHECK_IN.API_LEND_TOTAL_AMOUNT, COMM_PRODUCT_PRDID.PARENT_ID);
        leftJoin(COMM_PRODUCT_CHANNEL, STAT_CHECK_IN.CHANNEL_CODE.eq(COMM_PRODUCT_CHANNEL.CHANNEL_CODE));
        leftJoin(COMM_PRODUCT_PRDID, STAT_CHECK_IN.PRODUCT_ID.eq(COMM_PRODUCT_PRDID.PRDID.cast(String.class)));

        between(STAT_CHECK_IN.CREATE_DATE, request.getStartDate(), request.getEndDate());

        eq(STAT_CHECK_IN.PRODUCT_ID, request.getProductId());
        eq(STAT_CHECK_IN.CHANNEL_CODE, request.getChannelCode());
        eq(STAT_CHECK_IN.ACTIVITY_ID, request.getActivityId());
        eq(STAT_CHECK_IN.PLATFORM, request.getPlatform());
        eq(COMM_PRODUCT_CHANNEL.CHANNEL_GROUP, request.getChannelGroup());
        eq(COMM_PRODUCT_CHANNEL.SECONDARY_CHANNEL_GROUP, request.getSecondaryChannelGroup());

        orderBy(STAT_CHECK_IN.CREATE_DATE, Sort.Direction.DESC);

        DecimalFormat format = new DecimalFormat("#.##%");

        return search(pageable, record -> {
            StatCheckInResponse response = new StatCheckInResponse();
            response.setCreateDate(record.get(STAT_CHECK_IN.CREATE_DATE));
            response.setPlatform(record.get(STAT_CHECK_IN.PLATFORM));
            response.setProductId(record.get(STAT_CHECK_IN.PRODUCT_ID));
            response.setChannelCode(record.get(STAT_CHECK_IN.CHANNEL_CODE));
            response.setActivityId(record.get(STAT_CHECK_IN.ACTIVITY_ID));
            response.setChannelGroup(record.get(COMM_PRODUCT_CHANNEL.CHANNEL_GROUP));
            response.setSecondaryChannelGroup(record.get(COMM_PRODUCT_CHANNEL.SECONDARY_CHANNEL_GROUP));
            response.setH5ViewPageCount(record.get(STAT_CHECK_IN.H5_VIEW_PAGE_COUNT));
            response.setH5CheckInCount(record.get(STAT_CHECK_IN.H5_CHECK_IN_COUNT));
            response.setH5RegisterCountA(record.get(STAT_CHECK_IN.H5_REGISTER_COUNT_A));
            response.setH5RegisterCountB(record.get(STAT_CHECK_IN.H5_REGISTER_COUNT_B));
            response.setLoginCount(record.get(STAT_CHECK_IN.LOGIN_COUNT));
            response.setAppRegisterCountA(record.get(STAT_CHECK_IN.APP_REGISTER_COUNT_A));
            response.setAppRegisterCountB(record.get(STAT_CHECK_IN.APP_REGISTER_COUNT_B));
            response.setUvPhone(record.get(STAT_CHECK_IN.UV_PHONE));
            response.setUvApi(record.get(STAT_CHECK_IN.UV_API));
            response.setApiApplyCount(record.get(STAT_CHECK_IN.API_APPLY_COUNT));
            response.setApiLendCount(record.get(STAT_CHECK_IN.API_LEND_COUNT));
            response.setApiLendAmount(record.get(STAT_CHECK_IN.API_LEND_TOTAL_AMOUNT));
            response.setParentProductId(record.get(COMM_PRODUCT_PRDID.PARENT_ID));

            String checkInRate = "0.00%";
            String globalH5CheckInRate = "0.00%";
            String parentProductH5CheckInRate = "0.00%";
            String loginRate = "0.00%";

            if(response.getH5ViewPageCount() != 0){
                checkInRate = format.format(response.getH5CheckInCount().doubleValue() / response.getH5ViewPageCount().doubleValue());
            }

            if(response.getH5CheckInCount() != 0){
                globalH5CheckInRate = format.format(response.getH5RegisterCountA().doubleValue() / response.getH5CheckInCount().doubleValue());
                parentProductH5CheckInRate = format.format(response.getH5RegisterCountB().doubleValue() / response.getH5CheckInCount().doubleValue());
                loginRate = format.format(response.getLoginCount().doubleValue() / response.getH5CheckInCount().doubleValue());
            }

            response.setCheckInRate(checkInRate);
            response.setGlobalH5CheckInRate(globalH5CheckInRate);
            response.setParentProductH5CheckInRate(parentProductH5CheckInRate);
            response.setLoginRate(loginRate);

            return response;

        });
    }

    @Override
    public List<StatCollaborateCheckIn> findAllByCreateDateAndChannelIn(List<Integer> channels, Date date) {
        fields(STAT_CHECK_IN.fields());
        eq(STAT_CHECK_IN.CREATE_DATE, date);
        in(STAT_CHECK_IN.CHANNEL_CODE, channels);
        return search(StatCollaborateCheckIn.class);
    }

    @Override
    public Integer getUv(java.util.Date settlementTime, Long channelId) {
        Integer uv = dslContext.select(DSL.sum(STAT_CHECK_IN.UV_PHONE)).from(STAT_CHECK_IN)
                .where(STAT_CHECK_IN.CREATE_DATE.eq(new java.sql.Date(settlementTime.getTime()))).and(STAT_CHECK_IN.CHANNEL_CODE.eq(channelId.intValue()))
                .fetchOne().into(Integer.class);
        return uv;
    }
}
